<?php 
namespace frontend\controllers;



use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;




use frontend\models\Position;  // 职位表
use frontend\models\PositionType;  // 职位类型表
use frontend\models\ComponyMsg;  // 公司信息 表
use frontend\models\Recruit;  // // 公司发布的职位信息表
use frontend\models\Apply;  // // 应聘表



class IndexController extends Controller
{


	public function init()
    {
        parent::init();
        session_start();
        $get=Yii::$app->request->get('r');
        if($get!='login/index')
        {
            if (!isset($_SESSION['user']) ){
            	// echo "没有session";
                echo '<script>alert("请先登录");location.href="?r=login/index"</script>';
            }
        }
    }

    
	public $layout='index';
    public $enableCsrfValidation = false; //   ajax  关闭csrf验证

    //   职位 首页
    public function actionIndex(){

         $spc = Yii::$app->request->get('spc',1); // 1职位   4公司
//        echo'AAA';

        $kd = Yii::$app->request->get('kd',''); //条件
//        echo',,,';


        $city = Yii::$app->request->get('city','');//接收要搜索的城市
//        echo'AAA';
        $position_type_id = Yii::$app->request->get('position_type_id');//接收要搜索的 职业类型




        //----------------- 左侧数据----------------------
        $position_type_arr = PositionType::find()->asArray()->all();  // 所有职位类型  数组
        // 再根据职位类型 查询 职位
        foreach($position_type_arr as $k=>$v){
            $position_type_arr[$k]['position_arr'] =  Position::find()->where(['position_type_id'=>$v['position_type_id']])->asArray()->all();
        }
        //----------------- 左侧数据----------------------


        // 判断  $spc   1 ? 4

        //   1   position_name  职位名称    position_name like  %$kd%
        //   4   compony_name   公司名称    compony_name like %$kd%



        //   中部   *****************  所有职位  *****************
        //  1 先查询 状态为1 的所有公司
        //  2 再根据 公司id 查询 职位
        if( $spc == '4'){
            $compony_msg = ComponyMsg::find()->where("status = 1 and  compony_name like '%$kd%'")->all();

        }else{
            $compony_msg = ComponyMsg::find()->where(['status'=>1])->all();
//        $compony_msg = ComponyMsg::find()->where(['status'=>1])->all();
        }




        $recruit_arr = [];
        foreach($compony_msg as $kk=>$vv){

            if( $spc == '1'){  //  公司下面的所有职位
                $recruit = Recruit::find()->where("user_id = $vv->user_id and work_area like '%$city%'  and position_name like '%$kd%' and position_type_id like'%$position_type_id%' and recruit_people >0")->all();


            }else{

                $recruit = Recruit::find()->where("user_id = $vv->user_id and recruit_people >0")->all();
            }
            array_push($recruit_arr, $recruit);
        }



        //  查询职位 类型
        foreach($recruit_arr as $kk=>$vv){
            foreach($vv as $k=>$v){
                $position_type = PositionType::findOne($v['position_type_id']);
                $v->position_type_id = $position_type->position_type_name;
            }
        }
        //   中部   *****************   所有职位  *****************





        /*地区展示列表*/
        $sql  = "SELECT * FROM e_area WHERE parent_id=0";
        $area = Yii::$app->db->createCommand($sql)->queryAll();
        return $this->render('index', ['position_type_arr'=>$position_type_arr, 'recruit_arr'=>$recruit_arr,'area'=>$area,'kd'=>$kd ]);



    }



    //查看职位 详情   //  投个简历
    public function actionShow(){

        $recruit_id = Yii::$app->request->get('recruit_id');  // 职位d

        // 根据 职位id 查询 职位信息
        $recruit = Recruit::findOne($recruit_id);


        // 查询  兼职类型
        $position_type =  PositionType::findOne(['position_type_id'=>$recruit->position_type_id]);
        $recruit->position_type_id = $position_type->position_type_name;

        // 查询  公司信息
        $compony_msg = ComponyMsg::findOne(['user_id'=>$recruit->user_id]);
//        print_r($compony_msg);

        return $this->render('show', ['data'=>$recruit, 'compony_msg'=>$compony_msg]);

    }



    // 应聘表
    public function actionAdd_apply(){

        $user_id = $_SESSION['user']['user_id']; // 当亲登录的id   从 session 中取得;

        $compony_id = Yii::$app->request->post('compony_id'); // 公司id
        $recruit_id = Yii::$app->request->post('recruit_id'); // 当前的职位id

        $apply = new Apply();  // 应聘表
        $apply->compony_id = $compony_id;
        $apply->recruit_id = $recruit_id;
        $apply->user_id = $user_id;


        if( $apply->insert() ){
            $res['status'] = 1;
            $res['msg'] = '简历已投进此公司..注意查看消息..';
        }else{
            $res['status'] = 0;
            $res['msg'] = '失败...';
        }

        echo json_encode($res);

    }

    // 投递简历消息
    public function actionDelivery(){
        $user_id = $_SESSION['user']['user_id'];
        $apply_arr = Apply::find()->where(['user_id'=>$user_id])->asArray()->all();  //当前用户已经投过的简历




        foreach($apply_arr as $k=>$v){
            $compony_msg = ComponyMsg::find()->where(['user_id'=>$v['compony_id']])->asArray()->one(); // 公司信息
            $apply_arr[$k]['compony_msg'] = $compony_msg;


            $recruit = Recruit::find()->where(  ['recruit_id'=>$v['recruit_id']] )->asArray()->one(); // 查询职位 信息
            $apply_arr[$k]['recruit'] = $recruit;


        }


        return $this->render('delivery',['apply_arr'=>$apply_arr]);
    }





    /*
     * 地区搜索
     * */
    public function actionAreasearch()
    {
        //ajax发送过来地区
        $pid = Yii::$app->request->get('id');
        $sql = 'SELECT * FROM e_area WHERE parent_id = '.$pid;
        $two_search = Yii::$app->db->createCommand($sql)->queryAll();
        if($two_search)
        {
            $news['msg']  = $two_search;
            $news['error'] = 1;
            echo json_encode($news);
        }
        else
        {
            $news['msg'] = '未查询到结果';
            $news['error'] = 0;
            echo json_encode($news);
        }
    }




}
 ?>